//
//  MineContentView.swift
//  我的 - 内容 View
//
//  Created by 千策万划-mini on 2025/4/29.
//

import Foundation

class MineContentView: UIView {
    
    private let list: [ModelBtn] = [
        ModelBtn(imageName: "icon_mine_order_repay", title: "Repayment", tag: 6),
        ModelBtn(imageName: "icon_mine_order_apply", title: "Applying", tag: 7),
        ModelBtn(imageName: "icon_mine_order_finish", title: "Finish", tag: 5)
    ]
    
    // MARK: - lazy
    lazy var imageTop: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(184), W(164))
        result.image = UIImage(named: "icon_mine_top")
        return result
    }()
    lazy var imageOrderBg: UIImageView = {
        let result = UIImageView()
        result.isUserInteractionEnabled = true
        result.widthHeight = XY(W(355), W(162))
        result.image = UIImage(named: "icon_mine_order_bg")
        return result
    }()
    lazy var labelPhone: OutlineLabel = {
        let result = OutlineLabel()
        return result
    }()
    lazy var labelOrderTitle: OutlineLabel = {
        let result = OutlineLabel()
        result.resetView(
            title: "Loan Order",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 16),
            shadowColor: UIColor(hex: "#4105A0"),
            outlineColor: UIColor(hex: "#3B0B9A")
        )
        return result
    }()
    lazy var btnAllOrder: UIButton = {
        let result = UIButton()
        let str = "All Order  "
        result.setTitleColor(COLOR_WHITE(), for: .normal)
        result.titleLabel?.font = UIFont.customMediumFont(size: 14)
        result.setAttributedTitle(str.setAttributedStringWithImage(imageName: "icon_arrow_detail", imageRect: CGRectMake(0, -7, 22, 22), index: str.count), for: .normal)
        result.sizeToFit()
        result.widthHeight = XY(result.width+23*2, result.height+19*2)
        result.tag = 1
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var imageAgree: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(W(345), W(73))
        result.setBackgroundImage(UIImage(named: "icon_mine_agree_bg"), for: .normal)
        result.tag = 2
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var imageService: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(W(345), W(143))
        result.setBackgroundImage(UIImage(named: "icon_mine_service_bg"), for: .normal)
        result.tag = 3
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.widthHeight = XY(SCREEN_WIDTH, 0)
        self.backgroundColor = .clear
        
        self.addSubView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 添加
    func addSubView() {
        addSubview(imageTop)
        imageTop.addSubview(labelPhone)
        addSubview(imageOrderBg)
        imageOrderBg.addSubview(labelOrderTitle)
        imageOrderBg.addSubview(btnAllOrder)
        addSubview(imageAgree)
        addSubview(imageService)
        resetView()
    }
    
    // MARK: - 点击事件
    @objc func btnClick(_ sender: UIControl) {
        feedback()
        sender.addClickAnimate()
        switch sender.tag {
        case 1:
            // 发送通知  4全部 7进行中 6待还款 5已结清
            NotificationCenter.default.post(name: Notification.Name(NOTICE_CHANGE_ORDER_TYPE), object: 4)
            if let tabBarController = self.fetchVC()?.tabBarController as? BaseTabbarController,
               let orderVC = tabBarController.viewControllers?[1] {
                tabBarController.selectedIndex = 1
                tabBarController.tabBarController(tabBarController, didSelect: orderVC)
            }
            break
        case 2: // loan agreement
            let vc = BaseWebVC()
            vc.webUrl = LOCAL_LOAN_AGREEMEENT
            GB_Nav?.pushViewController(vc, animated: true)
            break
        case 3: // service
            let vc = BaseWebVC()
            vc.webUrl = LOCAL_CUSTOMER_SERVICE
            GB_Nav?.pushViewController(vc, animated: true)
            break
        default:
            break
        }
    }
    @objc func btnItemClick(_ sender: UIControl) {
        feedback()
        sender.addClickAnimate()
        // 发送通知  4全部 7进行中 6待还款 5已结清
        NotificationCenter.default.post(name: Notification.Name(NOTICE_CHANGE_ORDER_TYPE), object: list[sender.tag].tag)
        if let tabBarController = self.fetchVC()?.tabBarController as? BaseTabbarController,
           let orderVC = tabBarController.viewControllers?[1] {
            tabBarController.selectedIndex = 1
            tabBarController.tabBarController(tabBarController, didSelect: orderVC)
        }
    }
    
    
    
    // MARK: - 重置界面
    func resetView() {
        imageTop.centerXTop = XY(width/2, 19)
        imageOrderBg.centerXTop = XY(width/2, imageTop.bottom+18)
        
        var phone = UnPackStr(GlobalData.sharedInstance.GB_UserModel?.cap)
        if phone.count >= 6 {
            let startIndex = phone.index(phone.startIndex, offsetBy: 2)
            let endIndex = phone.index(startIndex, offsetBy: min(4, phone.count - 2))
            let range = startIndex..<endIndex
            phone = phone.replacingCharacters(in: range, with: "****")
        }
        labelPhone.resetView(
            title: phone,
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 20),
            shadowColor: UIColor(hex: "#2D0084"),
            outlineColor: UIColor(hex: "#2D0084")
        )
        labelPhone.centerXBottom = XY(imageTop.width/2, imageTop.height-W(10))
        
        labelOrderTitle.leftTop = XY(W(26), W(15))
        
        let margin = 20.0
        for i in 0..<list.count {
            let model = list[i]
            let btn = UIButton()
            btn.widthHeight = XY((imageOrderBg.width-margin*2)/3, 0)
            
            let image = UIImageView()
            image.widthHeight = XY(W(50), W(50))
            image.image = UIImage(named: UnPackStr(model.imageName))
            image.centerXTop = XY(btn.width/2, 0)
            btn.addSubview(image)
            
            let label = OutlineLabel()
            label.resetView(
                title: UnPackStr(model.title),
                textColor: COLOR_WHITE(),
                font: UIFont.customMediumFont(size: 14),
                shadowColor: UIColor(hex: "#2D0084"),
                outlineColor: UIColor(hex: "#2D0084")
            )
            label.centerXTop = XY(btn.width/2, image.bottom+W(2))
            btn.addSubview(label)
            btn.height = label.bottom
            btn.leftTop = XY(margin+btn.width*CGFloat(i), labelOrderTitle.bottom+W(12))
            imageOrderBg.addSubview(btn)
            
            btn.tag = i
            btn.addTarget(self, action: #selector(btnItemClick(_:)), for: .touchUpInside)
        }
        
        btnAllOrder.rightCenterY = XY(imageOrderBg.width, labelOrderTitle.centerY)
        
        imageAgree.centerXTop = XY(width/2, imageOrderBg.bottom+W(8))
        imageService.centerXTop = XY(width/2, imageAgree.bottom+W(15))
        height = imageService.bottom+W(66)
    }
}
